﻿Imports DataAcessLayer
Imports BusinessLogic
Imports ZakaFrontEnd
Imports System.Data
Imports System.Data.SqlClient
Imports AjaxControlToolkit

Partial Public Class ZakaNovo
    Inherits System.Web.UI.Page

    Dim DAL As New DAL
    Dim BL As New BL

    Dim userID = DAL.ObterUserIDPorNome(User.Identity.Name).FirstOrDefault.IDUser

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            PreencheDados()

            btnSubmeterTrofeu.Enabled = False
            btnSubmeter.Enabled = False
            ddlVotaMVP.Enabled = False
            ddlTrofeu.Enabled = False

            PanelContainer.Visible = False

        End If

    End Sub

    Protected Function Activo(ByVal eval As String) As String

        Select Case eval
            Case ""

                Return "Votação Não Terminada"

            Case Else

                Return eval

        End Select

    End Function

#Region "Preenchimento"

    Protected Sub PreencheDados()

        'Dim ultimoJogo = DAL.ObterJogos.FirstOrDefault

        'grdJogos.DataSource = DAL.ObterJogos.ToList
        'grdJogos.DataBind()

        'grdRank.DataSource = DAL.ObterRankSP.ToList
        'grdRank.DataBind()

        'grdTrofeus.DataSource = DAL.ObterTrofeus
        'grdTrofeus.DataBind()

        gvJogos.DataSource = DAL.ObterJogos.ToList
        gvJogos.DataBind()

        gvRank.DataSource = DAL.ObterRankSP.ToList
        gvRank.DataBind()

        gvTrofeus.DataSource = DAL.ObterTrofeus
        gvTrofeus.DataBind()

        'gvEquipaA.DataSource = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ultimoJogo.IDJogos, 1)
        'gvEquipaA.DataBind()

        'gvEquipaB.DataSource = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ultimoJogo.IDJogos, 2)
        'gvEquipaB.DataBind()

    End Sub

    Protected Sub cmbListaTrofeus_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaTrofeus.Init

        ddlListaTrofeus.DataSource = DAL.ObterTrofeus
        ddlListaTrofeus.DataValueField = "IDTrofeu"
        ddlListaTrofeus.DataTextField = "NomeTrofeu"
        ddlListaTrofeus.DataBind()

    End Sub

    Protected Sub cmbListaJogos_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaJogos.Init

        ddlListaJogos.DataSource = DAL.ObterDatasJogos
        ddlListaJogos.DataValueField = "IDJogos"
        ddlListaJogos.DataTextField = "Data"
        ddlListaJogos.DataBind()
        ddlListaJogos.Items.Insert(0, "Escolhe uma Data")

    End Sub

    Protected Sub cmbVotaMVP_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlVotaMVP.Init

        'cmbVotaMVP.DataSource = DAL.ObterListaJogadoresSP(userID, cmbListaJogos.SelectedValue)
        'cmbVotaMVP.DataValueField = "IDUser"
        'cmbVotaMVP.DataTextField = "NomeUtilizador"
        'cmbVotaMVP.DataBind()

    End Sub

    Protected Sub ddlTrofeu_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlTrofeu.Init

        ddlTrofeu.DataSource = DAL.ObterListaJogadoresTrofSP(userID)
        ddlTrofeu.DataValueField = "IDUser"
        ddlTrofeu.DataTextField = "NomeUtilizador"
        ddlTrofeu.DataBind()

    End Sub

#End Region

#Region "Operações"

    Protected Sub btnSubmeter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmeter.Click

        If BL.VotarMVP(ddlVotaMVP.SelectedValue, ddlListaJogos.SelectedValue, userID) Then
            ddlVotaMVP.Enabled = False
            btnSubmeter.Enabled = False
        End If

    End Sub

    Protected Sub btnSubmeterTrofeu_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmeterTrofeu.Click

        If BL.Votar(ddlTrofeu.SelectedValue, ddlListaTrofeus.SelectedValue, userID) Then
            ddlTrofeu.Enabled = False
            btnSubmeterTrofeu.Enabled = False
        End If

    End Sub

    Protected Sub cmbListaTrofeus_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaTrofeus.SelectedIndexChanged

        If Not BL.VerificaDados(userID, ddlListaTrofeus.SelectedValue) Then

            btnSubmeterTrofeu.Enabled = True
            ddlTrofeu.Enabled = True
        Else

            btnSubmeterTrofeu.Enabled = False
            ddlTrofeu.Enabled = False
        End If

    End Sub

    Protected Sub cmbListaJogos_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaJogos.SelectedIndexChanged

        If ddlListaJogos.SelectedIndex <> 0 Then

            Dim participou = DAL.VerificaParticipacao(userID, ddlListaJogos.SelectedValue)
            Dim votou = BL.VerificaDadosMVP(userID, ddlListaJogos.SelectedValue)

            If participou = False _
            OrElse votou = True Then

                btnSubmeter.Enabled = False
                ddlVotaMVP.Enabled = False
            Else

                btnSubmeter.Enabled = True
                ddlVotaMVP.Enabled = True
            End If

            Dim source = DAL.ObterListaJogadoresSP(userID, ddlListaJogos.SelectedValue)

            ddlVotaMVP.DataSource = source
            ddlVotaMVP.DataValueField = "IDUser"
            ddlVotaMVP.DataTextField = "NomeUtilizador"
            ddlVotaMVP.DataBind()

        Else
            ddlVotaMVP.Items.Clear()
            ddlVotaMVP.Enabled = False
            btnSubmeter.Enabled = False

        End If

    End Sub

    Protected Sub lnkFechar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkFechar.Click

        PanelContainer.Visible = False
    End Sub

    'Protected Sub gvJogos_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvJogos.PageIndexChanging

    '    gvJogos.PageIndex = e.NewPageIndex

    '    gvJogos.DataSource = DAL.ObterJogos.ToList
    '    gvJogos.DataBind()

    'End Sub

#End Region

#Region "Grid Pop-Up"

    <System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Shared Function GetDynamicContent(ByVal contextKey As String) As String
        Dim sTemp As New StringBuilder()

        Dim cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("ZakaEntities").ToString())
        Dim cmd As New SqlCommand("FUT_GET_ConstEquipas", cnn)
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add(New SqlParameter("@IDJogos", System.Data.SqlDbType.VarChar, 50, "NomeUtilizador"))
        cmd.Parameters("@IDJogos").Value = Convert.ToInt32(contextKey)

        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds, "teste")
        Dim total As Integer = ds.Tables("teste").Rows.Count

        If total > 0 Then
            sTemp.Append("<table>")
            sTemp.Append("<tr><td><b>Equipas:</b></td></tr>")
            For i As Integer = 0 To total - 1
                sTemp.Append(("<tr><td>" & ds.Tables("teste").Rows(i)(0).ToString() & "</td>" & "<td>") + ds.Tables("teste").Rows(i)(1).ToString() & "</td></tr>")
            Next

            sTemp.Append("</table>")
        End If


        Return (sTemp.ToString())
    End Function

    ' Protected Sub grdJogos_RowCreated(ByVal sender As Object, ByVal e As Obout.Grid.GridRowEventArgs) Handles grdJogos.RowCreated

    'If e.Row.RowType = DataControlRowType.DataRow Then
    '    ' Programmatically reference the PopupControlExtender
    '    Dim pce As PopupControlExtender = TryCast(e.Row.FindControl("Image1_PopupControlExtender"), PopupControlExtender)

    '    Dim pce_ = e.Row.FindControl("Image1_PopupControlExtender")

    '    ' Set the BehaviorID
    '    Dim behaviorID As String = String.Concat("pce", e.Row.RowIndex)
    '    pce.BehaviorID = behaviorID

    '    ' Programmatically reference the Image control
    '    Dim i As Image = DirectCast(e.Row.Cells(1).FindControl("Image1"), Image)

    '    ' Add the clie nt-side attributes (onmouseover & onmouseout)
    '    Dim OnMouseOverScript As String = String.Format("$find('{0}').showPopup();", behaviorID)
    '    Dim OnMouseOutScript As String = String.Format("$find('{0}').hidePopup();", behaviorID)

    '    i.Attributes.Add("onmouseover", OnMouseOverScript)
    '    i.Attributes.Add("onmouseout", OnMouseOutScript)
    'End If
    ' End Sub

    'Protected Sub Selected(ByVal sender As Object, ByVal e As Obout.Grid.GridRecordEventArgs) Handles grdJogos.Select

    '    If Not e.RecordsCollection Is Nothing Then

    '        For Each record In e.RecordsCollection

    '            Dim ID = record("IDJogos")

    '            Dim source = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ID, 1)

    '            gvEquipaA.DataSource = source
    '            gvEquipaA.DataBind()

    '            gvEquipaB.DataSource = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ID, 2)
    '            gvEquipaB.DataBind()

    '            PanelContainer.Visible = True

    '        Next

    '    End If

    'End Sub

#End Region

End Class